﻿using System.Collections.Generic;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using System.Linq;

namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        /// <summary>
        /// GetJob is used to collect the tasks to finish the order creation
        /// </summary>
        /// <param name="FInterID"></param>
        /// <returns></returns>
        public DataSet ReadExData(string path)
        {

            ArrayList tbs = new ArrayList() { "A", "B", "C" };
            string a = (string)tbs[0];
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();

            try
            {
                string fileDone;
                string filestemp = path + @"\01Temp";
                string[] files = System.IO.Directory.GetFiles(filestemp);
                foreach (string file in files)
                {
                    fileDone = file.Replace(@"\01Temp\", @"\02Incoming\");
                    System.IO.File.Move(file, fileDone);
                }
                string files2p = path + @"\02Incoming";
                files = System.IO.Directory.GetFiles(files2p);

                if (files.Length > 0)
                {
                    string file = files[0];
                    Result.ReadXml(file);
                    Return.Merge(Result);
                    fileDone = file.Replace(@"\02Incoming\", @"\03Done\");
                    System.IO.File.Move(file, fileDone);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return Return;
        }
        private void CheckDirectory()
        {
            if (!System.IO.Directory.Exists("C:\\IntexReport"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport");
            }

            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport\\Data");
            }
            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data\\ITX"))
            {
                System.IO.Directory.CreateDirectory("C:\\ntexReport\\Data\\ITX");
            }
            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data\\ITX\\EXCEL"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport\\Data\\ITX\\EXCEL");
            }
            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data\\ITX\\01Temp"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport\\Data\\ITX\\01Temp");
            }
            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data\\ITX\\02Incoming"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport\\Data\\ITX\\02Incoming");
            }
            if (!System.IO.Directory.Exists("C:\\IntexReport\\Data\\ITX\\03Done"))
            {
                System.IO.Directory.CreateDirectory("C:\\IntexReport\\Data\\ITX\\03Done");
            }

        }
        public DataSet LoadExcelData(string path,string prefix= "ITXEX")
        { 
            DataSet SResult = new DataSet();  
            CheckDirectory();  
            string[] files = System.IO.Directory.GetFiles(path);
            foreach (string file in files)
            {
                string ShortName = file.Substring(file.LastIndexOf("\\") + 1);
                if (!ShortName.StartsWith(prefix))
                    continue;
                EventDispatcher.Instance.RaiseStatus("装载:" + ShortName + "开始", StatusEventType.StopProgress);
                if (System.IO.File.Exists(file))
                { 
                    SResult = exAgent.LoadDataTableDef(file, "", 200); 
                }
                break;
            }
            return SResult;
        } 
    }
}
